﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using NHibernate.Criterion;

namespace Cms.BLL.NetGather
{
    public class HtmlAgility
    {
        /// <summary>
        /// 要解析的地址
        /// </summary>
        private string Url { get; set; }
        /// <summary>
        /// HTML 内容
        /// </summary>
        private string HtmlContent { get; set; }

        public HtmlAgility(string html)
        {
            this.HtmlContent = html;
            Document = new HtmlAgilityPack.HtmlDocument();
            Document.LoadHtml(HtmlContent);
        }

        private HtmlAgilityPack.HtmlDocument Document = null;

        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="Title">规则名称</param>
        /// <param name="Rule">规则</param>
        /// <param name="AdRule">广告规则</param>
        /// <returns></returns>
        public string ParseHtml(string Title, string Rule,string AdRule)
        {
            string result = "";
            if (!String.IsNullOrEmpty(Rule) && !String.IsNullOrEmpty(Title))
            {
                HtmlAgilityPack.HtmlNode Node = Document.DocumentNode.SelectSingleNode(Rule);
                if (Node != null)
                {
                    if (!String.IsNullOrEmpty(AdRule))
                    {
                        HtmlNode adNode = Node.SelectSingleNode(AdRule);
                        if (adNode != null)
                            adNode.Remove();
                    }                   
                    result = Node.InnerHtml;
                }
            }
            return result;
        }
        /// <summary>
        /// 解析一个多个规则
        /// </summary>
        /// <param name="listRule"></param>
        /// <returns></returns>
        public Dictionary<string, string> ParseHtml(ICollection<Model.Rssruledetail> listRule)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (Model.Rssruledetail model in listRule) {
                string content = ParseHtml(model.Title.ToString(), model.Rule,model.Adrule);
                if (!String.IsNullOrEmpty(content)) {
                    result.Add(model.Title, content);
                }
            }
            return result;
        }
        public Dictionary<string, string> ParseHtml(Model.Rssrules rules) {
            return ParseHtml(rules.Rules);
        }
        /// <summary>
        /// 解析一个网站
        /// </summary>
        /// <param name="SiteId"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ParseHtml(string SiteId) {
            BaseDao<Model.Rsssite, string> bll = new BaseDao<Model.Rsssite, string>();
            Model.Rsssite model=bll.GetById(SiteId,false);
            if (model != null)
            { 
                HtmlAgility ha = new HtmlAgility(model.Siteurl);
                BaseDao<Model.Rssrules,string> bllRule = new BaseDao<Model.Rssrules,string>();
                List<Model.Rssrules> listRule=bllRule.GetByCriteria(Expression.Eq("Siteid", model.Id));
                if (listRule.Count > 0) {
                    return ha.ParseHtml(listRule[0].Rules);
                }
            }            
            return null;
        }
    }
}
